package com.cr.backbone.hibenate;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 数据 查询参数实现
 * @project RaedNote
 * @version 1.0
 * @company
 * @fileDescribe
 * @fileType QueryElementImpl QueryElementImpl.java
 * @author xianghua.hu
 * @date 2012-2-26 下午08:17:38
 */
class QueryParamElmentImpl implements QueryParamElment {
	
	private String queryKey;
	private String queryValue;
	
	private String key;
	private OPERATOR operator;
	private TYPE type;
	
	public QueryParamElmentImpl() {}
	public QueryParamElmentImpl(String queryKey, String queryValue) {
		setQuery(queryKey,queryValue);
	}
//	public QueryParamElmentImpl(String key,OPERATOR operator,TYPE type,String queryValue){
//		 setQuery(key,operator,type,queryValue);
//	}
	
	public void setQuery(String queryKey,String queryValue) {
		String[] keys = queryKey.split("_");
		this.queryKey = queryKey;
		this.queryValue = queryValue;
		
		// 解析处理操作
		//
		if(keys!=null && keys.length>0){
			this.operator = str2operator(keys[0]);
			this.key = keys[1];
			this.type = str2type(keys[2]);
		}
	}

//	public void setQuery(String key,OPERATOR operator,TYPE type,String queryValue) {
//		this.key = key;
//		this.operator = operator;
//		this.type = type;
//		this.queryKey = operator2str(operator)+key + type2str(type);
//		this.queryValue = queryValue;
//	}
	
	public String getKey() {
		return key;
	}
	public Object getValue() {
		switch(getType()){
			case STRING:
				return getString();
			case INTEGER:
				return getInteger(0);
			case LONG:
				return getLong(0L);
			case FLOAT:
				return getFloat(0F);
			case DOUBLE:
				return getDouble(0D);
			case BOOLEAN:
				return getBoolean(true);
			case DATE:
				return getDate(new Date());
			case DATETIME:
				return getDateTime(new Date());
		}
		return getString();
	}
	
	public OPERATOR getOperator(){
		return operator;
	}
	
	public TYPE getType(){
		return type;
	}
	
	
	public String getQueryKey(){
		return this.queryKey;
	}
	public String getQueryValue(){
		return this.queryValue;
	}
	
	/**
	 * 返回查询值
	 * 
	 * @param defaultValue
	 * @return
	 */
	public String getString(){
		return queryValue;
	}
	/**
	 * 将查询值转化为整型
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Integer getInteger(Integer defaultValue){
		Integer value = defaultValue;
		try{
			value = Integer.valueOf(queryValue);
		}catch(Exception e){}
		return value;
	}
	/**
	 * 将查询值转化为长整型
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Long getLong(Long defaultValue){
		Long value = defaultValue;
		try{
			value = Long.valueOf(queryValue);
		}catch(Exception e){}
		return value;
	}
	/**
	 * 将查询值转化为浮点型
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Float getFloat(Float defaultValue){
		Float value = defaultValue;
		try{
			value = Float.valueOf(queryValue);
		}catch(Exception e){}
		return value;
	}
	/**
	 * 将查询值转化为双浮点型
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Double getDouble(Double defaultValue){
		Double value = defaultValue;
		try{
			value = Double.valueOf(queryValue);
		}catch(Exception e){}
		return value;
	}
	/**
	 * 将查询值转化为逻辑型
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Boolean getBoolean(Boolean defaultValue){
		Boolean value = defaultValue;
		try{
			value = Boolean.valueOf(queryValue);
		}catch(Exception e){}
		return value;
	}
	/**
	 * 将查询值转化为日期，格式：yyyy-MM-dd
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Date getDate(Date defaultValue){
		return getDate(defaultValue,"yyyy-MM-dd");
	}
	/**
	 * 将查询值转化为日期，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @param defaultValue
	 * @return
	 */
	public Date getDateTime(Date defaultValue){
		return getDate(defaultValue,"yyyy-MM-dd HH:mm:ss");
	}
	/**
	 * 将查询值转化为日期
	 * 
	 * @param defaultValue
	 *            默认值
	 * @param pattern
	 *            日期格式，
	 * @return
	 */
	public Date getDate(Date defaultValue,String pattern){
		Date value = defaultValue;
		if(pattern == null || pattern.equals("")){
			pattern = "yyyy-MM-dd";
		}
		try{
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			value =	sdf.parse(queryValue);
		}catch(Exception e){}
		return value;
	}
	
	
	/**
	 * 将操作转化为字符串，用于拼接queryKey
	 * 
	 * @param operator
	 * @return
	 */
	private String operator2str(OPERATOR operator){
		switch(operator){
			case EQUAL:
				return "";
			case LIKE:
				return "lk_";
			case GREAT:
				return "gt_";
			case LESS:
				return "lt_";
			case GREAT_EQUAL:
				return "ge_";
			case LESS_EQUAL:
				return "le_";
			case IS_NULL:
				return "nu_";
			case NOT_EQUAL:
				return "ne_";
			case NOT_NULL:
				return "nn_";
			default:
				return "";
		}
	}
	
	/**
	 * 将操作字符串转化为操作
	 * 
	 * @param operator
	 * @return
	 */
	private OPERATOR str2operator(String operator){
		if(operator == null || operator.equals("")){
			return OPERATOR.EQUAL;
		}else if(operator.equals("lk")){
			return OPERATOR.LIKE;
		}else if(operator.equals("gt")){
			return OPERATOR.GREAT;
		}else if(operator.equals("ge")){
			return OPERATOR.GREAT_EQUAL;
		}else if(operator.equals("lt")){
			return OPERATOR.LESS;
		}else if(operator.equals("le")){
			return OPERATOR.LESS_EQUAL;
		}else if(operator.equals("nu")){
			return OPERATOR.IS_NULL;
		}else if(operator.equals("ne")){
			return OPERATOR.NOT_EQUAL;
		}else if(operator.equals("nn")){
			return OPERATOR.NOT_NULL;
		}else{
			return OPERATOR.EQUAL;
		}
	}
	
	/**
	 * 将类型转化为字符串，用于拼接queryKey
	 * 
	 * @param type
	 * @return
	 */
	private String type2str(TYPE type){
		switch(type){
			case INTEGER:
				return "@i";
			case LONG:
				return "@l";
			case FLOAT:
				return "@f";
			case DOUBLE:
				return "@d";
			case BOOLEAN:
				return "@b";
			case DATE:
				return "@D";
			case DATETIME:
				return "@t";
			default:
				return "";
		}
	}
	
	/**
	 * 将类型字符串转化为类型
	 * 
	 * @param type
	 * @return
	 */
	private TYPE str2type(String type){
		if(type == null || type.equals("")){
			return TYPE.STRING;
		}else if(type.startsWith("i")){
			return TYPE.INTEGER;
		}else if(type.startsWith("l")){
			return TYPE.LONG;
		}else if(type.startsWith("f")){
			return TYPE.FLOAT;
		}else if(type.startsWith("d")){
			return TYPE.DOUBLE;
		}else if(type.startsWith("b")){
			return TYPE.BOOLEAN;
		}else if(type.startsWith("D")){
			return TYPE.DATE;
		}else if(type.startsWith("t")){
			return TYPE.DATETIME;
		}else{
			return TYPE.STRING;
		}
	}
	
//
//
//
//public String toQueryString(){
//	if(query==null || query.isEmpty()){
//		return "Query is Empty";
//	}
//	StringBuffer sb = new StringBuffer("QueryParam:[");
//	for(QueryElement qe : query.values()){
//		sb.append(qe.getKey()).append("=").append(qe.getQueryValue()).append(",");
//	}
//	return sb.append("]").toString();
}
 